#include "signalisation_serveur.h"

//pour recherche avec motif
#include <sys/types.h>
#include <regex.h>


/**
 * Routine appele à la reception de la requete "list" par la partie serveur de notre programme.
 * @param socksrc La socket de la partie serveur de notre programme
 * @param sockdest La socket du client attendant la reponse
 * @param list Adresse de la liste des fichiers
 *
 * @return RT_OK en cas de reussite, sinon RT_ERR.
 */
return_t cmd_list_handler(int socksrc, int sockdest, filelist_p list){
	if (list != NULL){
		if (list->size == 0){
			int ret_send;
			SOCKADDR_IN adddest, addsrc;
			char file_msg[BUFSIZE];
			element *node_list = list->first;

			//addr_from_sock(socksrc, &addsrc);
			//addr_from_sock(sockdest, &adddest);
			char *file_path;

			while(node_list != list->last){
				sprintf(file_msg, "file %s %s %d (%s:%d)", node_list->data->name, node_list->data->key,
						node_list->data->size, inet_ntoa(addsrc.sin_addr), htons(addsrc.sin_port));
				ret_send = send(sockdest, file_msg, sizeof(file_msg), 0);
				if (ret_send == -1)
					perror("send");
				sleep(1);
			}
			return RT_OK;
		}
		fprintf(stdout, "list fichiers vide\n");
	}
	fprintf(stdout, "list fichiers non initialise\n");
	return RT_ERR;
}

return_t cmd_checksum_handler(int sockdest, char *key, int begin, int end, int blocksize, filelist_p list, const char *dirpath){
	if (list != NULL){
		char checksum_msg[3000];
		char *name = fgetname(list, key);
		char *pathfile = fpath(name, dirpath);
		vector_p v = fmd5sum_bloc(pathfile, begin, end, blocksize);
		int ret_send, i, nb_sum = vector_len(v);
		char checksum [nb_sum][BUFSIZE];

		sprintf(checksum_msg, "checksum %s %d %d %d ", key, blocksize, begin, end);
		for(i=0; i<nb_sum; i++){
			strcpy(checksum[i], vector_get(v, i));
			//printf("%s\n", checksum[i]);
			strcat(checksum_msg, checksum[i]);
			strcat(checksum_msg, " ");
		}
		//printf("%s\n", checksum_msg);
		ret_send = send(sockdest, checksum_msg, sizeof(checksum_msg), 0);
		if (ret_send == -1)
			perror("send");
		free(pathfile);

		return RT_OK;
	}
	return RT_ERR;
}

int accept_motif(const char *motif, const char *name){
	regmatch_t *pmatch;
	size_t nmatche;
	regex_t preg;
	int ret;
	if ((ret = regcomp(&preg, motif, REG_EXTENDED)) != 0){
		return 0;
	}
	if (regexec(&preg, name, nmatche, pmatch, 1) ==  0){
		return 1;
	}
	return 0;
}


// nouvelle filelist allouee
filelist_p list_with_motif(const char *motif, filelist_p oldlist){
	filelist_p newlist = flist_new();
	element *tmp_oldlist = oldlist->first;

	regmatch_t *pmatch;
	size_t nmatche;
	regex_t preg;

	//compilation d'une expression reguliere
	int ret;
	if ((ret = regcomp(&preg, motif, REG_EXTENDED)) != 0){
		//printf("prob compil %d %s\n", ret, motif);
		return NULL;
	}
	//parcous de la liste des fichiers
	while (tmp_oldlist != oldlist->last){
		if (regexec(&preg, tmp_oldlist->data->name, nmatche, pmatch, 1) ==  0){
			flist_insert(newlist, tmp_oldlist->data);
		}
		tmp_oldlist = tmp_oldlist->next;
	}

	//dernier element
	if (regexec(&preg, tmp_oldlist->data->name, nmatche, pmatch, 1) ==  0){
		flist_insert(newlist, tmp_oldlist->data);
	}

	return newlist;
}
